package com.example.demo.GPS51Test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.DateTest.util.DateUtil;
import com.example.demo.HTTPTest.util.OKHttpUtil;
import com.example.demo.MD5Test.util.Md5Util;
import com.example.demo.config.dateUtil.DateUtils;
import org.apache.commons.lang3.ObjectUtils;

import java.io.IOException;
import java.util.*;

/**
 * @author LHH
 * @date 2025/5/8
 */
public class Gps51ApiTest {

    public static void main(String[] args) throws Exception {
        String urlPrefix = "https://gps51.com/webapi?action=";
        getData(urlPrefix);
    }

    public static Map<String, Object> getData(String urlPrefix) throws Exception {
        // --- 登录 ---
//        String username = "13057668120";
//        String pwd = "Yhnf2020";
//        String password = Md5Util.md5(pwd);
//        String url = urlPrefix + "login";
//        return getToken(url,username,password);

        // [Token/DeviceId]
        String token = "c24155f6a7870db9313d50829aff72fd";
        String deviceId = "15760027391";

        // --- 设备列表 ---
//        String username = "13057668120";

//        String url = urlPrefix + "querymonitorlist&&token="+token;
//        return getDeviceList(url,username);

        // --- 轨迹查询 ---
        // yyyy-MM-dd HH:mm:ss
        String begintime = "2025-05-07 00:00:00";
        String endtime = "2025-05-07 23:59:59";
        String url = urlPrefix + "querytracks&&token=" + token;
        return getTrajectory(url, deviceId, begintime, endtime);

        // --- 回放地址 ---
//        double channel = 1;
//        String starttime = "2025-05-07 00:00:00";
//        String endtime = "2025-05-07 23:59:59";
//        String url = urlPrefix + "querymedialist_sync&&token=" + token;
//        return getPlaybackAddress(url, deviceId, channel, starttime, endtime);

        // --- 直播地址 ---
//        List<String> channels = Arrays.asList("1","2","3","4");
//        double datatype = 0;
//        // 网页建议用 flvs ,app和小程序用 rtmp
//        String playtype = "flvs";
//        String url = urlPrefix + "startvideos_sync&&token=" + token;
//        return getLiveAddress(url, deviceId, channels, datatype, playtype);

        // --- 最后位置 ---
//        List<String> deviceids = Arrays.asList("15760027391");
//        Long lastquerypositiontime = 0L;
//        String url = urlPrefix + "lastposition&&token=" + token;
//        return getLastPosition(url,deviceids,lastquerypositiontime);

    }

    public static Map<String, Object> getToken(String url, String username, String password) throws Exception {
        JSONObject bodyJson = new JSONObject();
        bodyJson.put("username", username);
        bodyJson.put("password", password);
        bodyJson.put("from", "WEB");
        bodyJson.put("type", "USER");
        Map<String, Object> doPost = OKHttpUtil.dopost(url, bodyJson);
        System.out.println(doPost);
        JSONObject result = JSONObject.parseObject(doPost.get("result").toString());
        String token = result.getString("token");
        System.out.println(token);
        return doPost;

    }

    public static Map<String, Object> getDeviceList(String url, String username) throws Exception {
        JSONObject bodyJson = new JSONObject();
        bodyJson.put("username", username);
        Map<String, Object> doPost = OKHttpUtil.dopost(url, bodyJson);
        System.out.println(doPost);
        JSONObject result = JSONObject.parseObject(doPost.get("result").toString());
        System.out.println(result);
        return doPost;
    }

    public static Map<String, Object> getTrajectory(String url, String deviceid, String begintime, String endtime) throws Exception {
        JSONObject bodyJson = new JSONObject();
        bodyJson.put("deviceid", deviceid);
        bodyJson.put("begintime", begintime);
        bodyJson.put("endtime", endtime);
        bodyJson.put("timezone", 8);
        Map<String, Object> doPost = OKHttpUtil.dopost(url, bodyJson);
        JSONObject result = JSONObject.parseObject(doPost.get("result").toString());
        JSONArray recordArray = result.getJSONArray("records");
        JSONObject jsonObject = new JSONObject();

        for (Object record : recordArray) {
            JSONObject recordJson = JSONObject.parseObject(record.toString());
            System.out.println(recordJson.getString("updatetime"));
            Date updatetime = recordJson.getDate("updatetime");
            System.out.println(updatetime);
            System.out.println(DateUtil.getDate(updatetime));
            String dateTime = DateUtil.getDateTime(updatetime);
            System.out.println(dateTime);
            long diff = DateUtil.dateDiff(DateUtil.DateSort.MINUTE.getName(), new Date(), updatetime);
            System.out.println(diff);
            System.out.println("------------");

            jsonObject.put("max", ObjectUtils.isEmpty(jsonObject.get("max")) || DateUtil.dateDiff(DateUtil.DateSort.SECOND.getName(),jsonObject.getDate("max"), updatetime) <= 0 ? dateTime : jsonObject.get("max"));
            jsonObject.put("min", ObjectUtils.isEmpty(jsonObject.get("min")) || DateUtil.dateDiff(DateUtil.DateSort.SECOND.getName(),jsonObject.getDate("min"), updatetime) >= 1 ? dateTime : jsonObject.get("min"));
        }
        System.out.println("*****************");
        long seconds = DateUtil.dateDiff(DateUtil.DateSort.SECOND.getName(), jsonObject.getDate("max"), jsonObject.getDate("min"));
        System.out.println(Math.round(seconds / 60.0 / 60.0 * 100) / 100.0);
        System.out.println(jsonObject);
        return doPost;
    }

    public static Map<String, Object> getPlaybackAddress(String url, String deviceid, double channel, String starttime, String endtime) throws Exception {
        JSONObject bodyJson = new JSONObject();
        bodyJson.put("deviceid", deviceid);
        bodyJson.put("channel", channel);
        bodyJson.put("starttime", starttime);
        bodyJson.put("endtime", endtime);
        bodyJson.put("mediatype", 0);
        bodyJson.put("storetype", 0);
        bodyJson.put("timezone", 8);
        Map<String, Object> doPost = OKHttpUtil.dopost(url, bodyJson);
        System.out.println(doPost);
        JSONObject result = JSONObject.parseObject(doPost.get("result").toString());
        System.out.println(result);
        return doPost;
    }

    public static Map<String, Object> getLiveAddress(String url, String deviceid, List<String> channels, double datatype, String playtype) throws IOException {
        JSONObject bodyJson = new JSONObject();
        bodyJson.put("deviceid", deviceid);
        bodyJson.put("channels", channels);
        bodyJson.put("datatype", datatype);
        bodyJson.put("playtype", playtype);
        Map<String, Object> doPost = OKHttpUtil.dopost(url, bodyJson);
        System.out.println(doPost);
        JSONObject result = JSONObject.parseObject(doPost.get("result").toString());
        System.out.println(result);
        return doPost;
    }

    private static Map<String, Object> getLastPosition(String url, List<String> deviceids, Long lastquerypositiontime) throws IOException {
        JSONObject bodyJson = new JSONObject();
        bodyJson.put("deviceids", deviceids);
        bodyJson.put("lastquerypositiontime", lastquerypositiontime);
        Map<String, Object> doPost = OKHttpUtil.dopost(url, bodyJson);
        System.out.println(doPost);
        JSONObject result = JSONObject.parseObject(doPost.get("result").toString());
        System.out.println(result);
        return doPost;
    }

}
